<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>HTML 中的 JavaScript | Freeisms</title>
    <meta name="generator" content="VuePress 1.7.1">
    <link rel="icon" href="/favicon.ico">
    <link rel="manifest" href="/manifest.json">
    <link rel="apple-touch-icon" href="/apple-touch-icon.png">
    <meta name="description" content="记录个人前端技术成长点滴">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    
    <link rel="preload" href="/assets/css/0.styles.9efbc543.css" as="style"><link rel="preload" href="/assets/js/app.e45e7f79.js" as="script"><link rel="preload" href="/assets/js/4.1dc261f9.js" as="script"><link rel="preload" href="/assets/js/31.04c169d6.js" as="script"><link rel="prefetch" href="/assets/js/10.910e44c4.js"><link rel="prefetch" href="/assets/js/11.3cb7fbc0.js"><link rel="prefetch" href="/assets/js/12.e84e4441.js"><link rel="prefetch" href="/assets/js/13.a20b1e0e.js"><link rel="prefetch" href="/assets/js/14.5bb81eb0.js"><link rel="prefetch" href="/assets/js/15.9ac9c7b5.js"><link rel="prefetch" href="/assets/js/16.e65d6e8b.js"><link rel="prefetch" href="/assets/js/17.3bcb37e9.js"><link rel="prefetch" href="/assets/js/18.238c27dc.js"><link rel="prefetch" href="/assets/js/19.0bf085fb.js"><link rel="prefetch" href="/assets/js/20.af32ca4b.js"><link rel="prefetch" href="/assets/js/21.d7d0a4bf.js"><link rel="prefetch" href="/assets/js/22.fb0bd41c.js"><link rel="prefetch" href="/assets/js/23.e40c513c.js"><link rel="prefetch" href="/assets/js/24.df877caa.js"><link rel="prefetch" href="/assets/js/25.ff943ee7.js"><link rel="prefetch" href="/assets/js/26.6975fab4.js"><link rel="prefetch" href="/assets/js/27.49355cdd.js"><link rel="prefetch" href="/assets/js/28.08ac8c0a.js"><link rel="prefetch" href="/assets/js/29.864244b6.js"><link rel="prefetch" href="/assets/js/30.9fde9cc1.js"><link rel="prefetch" href="/assets/js/32.57021f58.js"><link rel="prefetch" href="/assets/js/33.09502731.js"><link rel="prefetch" href="/assets/js/34.8a018286.js"><link rel="prefetch" href="/assets/js/35.097f93be.js"><link rel="prefetch" href="/assets/js/36.37ccdfeb.js"><link rel="prefetch" href="/assets/js/37.11976f3e.js"><link rel="prefetch" href="/assets/js/38.98243aad.js"><link rel="prefetch" href="/assets/js/39.a3c78447.js"><link rel="prefetch" href="/assets/js/40.7be6398d.js"><link rel="prefetch" href="/assets/js/41.33d492ee.js"><link rel="prefetch" href="/assets/js/42.f1c675e6.js"><link rel="prefetch" href="/assets/js/43.bbda546b.js"><link rel="prefetch" href="/assets/js/44.2dc401cd.js"><link rel="prefetch" href="/assets/js/45.33bd1151.js"><link rel="prefetch" href="/assets/js/46.b6b09012.js"><link rel="prefetch" href="/assets/js/47.55feb448.js"><link rel="prefetch" href="/assets/js/48.fc9dc909.js"><link rel="prefetch" href="/assets/js/49.fd6d33c4.js"><link rel="prefetch" href="/assets/js/5.ac586c16.js"><link rel="prefetch" href="/assets/js/50.8e33b7f9.js"><link rel="prefetch" href="/assets/js/51.fd278c0a.js"><link rel="prefetch" href="/assets/js/52.fbd66a3b.js"><link rel="prefetch" href="/assets/js/53.f829ac68.js"><link rel="prefetch" href="/assets/js/54.7c7e5ec8.js"><link rel="prefetch" href="/assets/js/55.42e87201.js"><link rel="prefetch" href="/assets/js/56.2301ffbd.js"><link rel="prefetch" href="/assets/js/57.6d5ed1a9.js"><link rel="prefetch" href="/assets/js/58.8dc29eab.js"><link rel="prefetch" href="/assets/js/59.722945c8.js"><link rel="prefetch" href="/assets/js/6.64876e20.js"><link rel="prefetch" href="/assets/js/60.c43354e4.js"><link rel="prefetch" href="/assets/js/61.b9d1ad1d.js"><link rel="prefetch" href="/assets/js/62.d61bfc10.js"><link rel="prefetch" href="/assets/js/63.d84a88b5.js"><link rel="prefetch" href="/assets/js/64.0f25c14d.js"><link rel="prefetch" href="/assets/js/7.d74ac93a.js"><link rel="prefetch" href="/assets/js/8.6c8e2482.js"><link rel="prefetch" href="/assets/js/9.085dc649.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.4e554982.js"><link rel="prefetch" href="/assets/js/vendors~notification.4b668bb8.js">
    <link rel="stylesheet" href="/assets/css/0.styles.9efbc543.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">Freeisms</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/my-docs/js/" class="nav-link router-link-active">
  JavaScript
</a></div><div class="nav-item"><a href="/my-docs/design-pattern/" class="nav-link">
  设计模式
</a></div><div class="nav-item"><a href="/my-docs/leetcode/" class="nav-link">
  LeetCode
</a></div><div class="nav-item"><a href="/my-docs/library/vue-juejin-comment.html" class="nav-link">
  开源库
</a></div><div class="nav-item"><a href="https://www.striveforus.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  博客
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <a href="https://github.com/Fengfengfeng-up/personal-docs" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/my-docs/js/" class="nav-link router-link-active">
  JavaScript
</a></div><div class="nav-item"><a href="/my-docs/design-pattern/" class="nav-link">
  设计模式
</a></div><div class="nav-item"><a href="/my-docs/leetcode/" class="nav-link">
  LeetCode
</a></div><div class="nav-item"><a href="/my-docs/library/vue-juejin-comment.html" class="nav-link">
  开源库
</a></div><div class="nav-item"><a href="https://www.striveforus.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  博客
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <a href="https://github.com/Fengfengfeng-up/personal-docs" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>基础</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/my-docs/js/" aria-current="page" class="sidebar-link">写在前面</a></li><li><a href="/my-docs/js/what-is-javascript.html" class="sidebar-link">什么是 JavaScript</a></li><li><a href="/my-docs/js/javascript-in-html.html" aria-current="page" class="active sidebar-link">HTML 中的 JavaScript</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/my-docs/js/javascript-in-html.html#script-元素" class="sidebar-link">&lt;script&gt; 元素</a></li><li class="sidebar-sub-header"><a href="/my-docs/js/javascript-in-html.html#noscript-元素" class="sidebar-link">&lt;noscript&gt; 元素</a></li><li class="sidebar-sub-header"><a href="/my-docs/js/javascript-in-html.html#最佳实践" class="sidebar-link">最佳实践</a></li></ul></li><li><a href="/my-docs/js/language-basics.html" class="sidebar-link">语言基础</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="html-中的-javascript"><a href="#html-中的-javascript" class="header-anchor">#</a> HTML 中的 JavaScript</h1> <h2 id="script-元素"><a href="#script-元素" class="header-anchor">#</a> <code>&lt;script&gt;</code> 元素</h2> <p>在 JavaScript 早期，Netscape 公司创造出了 <code>&lt;script&gt;</code> 元素，用以将 JavaScript 插入到 HTML 页面中，现已加入 HTML 规范。它主要有以下属性和特点。</p> <h3 id="可选属性"><a href="#可选属性" class="header-anchor">#</a> 可选属性</h3> <table><thead><tr><th style="text-align:center;">属性名</th> <th style="text-align:center;">默认值</th> <th style="text-align:center;">用途</th> <th style="text-align:center;">作用脚本类型</th></tr></thead> <tbody><tr><td style="text-align:center;">async</td> <td style="text-align:center;"><code>false</code></td> <td style="text-align:center;">异步执行脚本</td> <td style="text-align:center;">外部</td></tr> <tr><td style="text-align:center;">charset</td> <td style="text-align:center;"><code>&quot;&quot;</code></td> <td style="text-align:center;">指定代码字符集（不常用）</td> <td style="text-align:center;">外部 / 行内</td></tr> <tr><td style="text-align:center;">crossorigin</td> <td style="text-align:center;"><code>null</code></td> <td style="text-align:center;">配置请求的 <a href="https://developer.mozilla.org/zh-CN/docs/Glossary/CORS" target="_blank" rel="noopener noreferrer">CORS<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 设置</td> <td style="text-align:center;">外部</td></tr> <tr><td style="text-align:center;">defer</td> <td style="text-align:center;"><code>false</code></td> <td style="text-align:center;">推迟执行脚本</td> <td style="text-align:center;">外部 / 行内（ IE7 及以下）</td></tr> <tr><td style="text-align:center;">integrity</td> <td style="text-align:center;"><code>&quot;&quot;</code></td> <td style="text-align:center;">检查获取的资源是否被篡改</td> <td style="text-align:center;">外部</td></tr> <tr><td style="text-align:center;">language</td> <td style="text-align:center;"></td> <td style="text-align:center;"><s>表示代码块中的脚本语言</s> （已废弃）</td> <td style="text-align:center;"></td></tr> <tr><td style="text-align:center;">src</td> <td style="text-align:center;"><code>&quot;&quot;</code></td> <td style="text-align:center;">表示要执行的外部文件地址</td> <td style="text-align:center;">外部</td></tr> <tr><td style="text-align:center;">type</td> <td style="text-align:center;"><code>&quot;&quot;</code></td> <td style="text-align:center;">表示脚本语言的 <a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types" target="_blank" rel="noopener noreferrer">MIME 类型<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></td> <td style="text-align:center;">外部 / 行内</td></tr></tbody></table> <h3 id="特点"><a href="#特点" class="header-anchor">#</a> 特点</h3> <ol><li>可以在 <code>&lt;script&gt;</code> 元素内嵌入 JavaScript 代码，也可通过设置它的 <code>src</code> 属性中引入外部 JavaScript 文件。</li> <li>浏览器解析使用 <code>src</code> 属性的 <code>&lt;script&gt;</code> 元素时，会向该属性值指代的地址发送一个 GET 请求以取得相应资源，这个请求不受浏览器同源策略限制，返回并被执行的 JavaScript 则受限制。</li> <li>使用 <code>defer</code> 属性的脚本会推迟到文档渲染完毕后再执行。多个推迟脚本原则上按照它们在页面中出现的顺序执行。</li> <li>使用 <code>async</code> 属性的异步脚本不需要等待其他脚本，同时也不阻塞文档渲染，不能保证按照它们在页面中出现的顺序执行。</li> <li>所有不使用 <code>async</code> / <code>defer</code> 属性的 <code>&lt;script&gt;</code> 元素会按照它们出现的顺序被解释。</li> <li>包含在 <code>&lt;script&gt;</code> 元素内的代码会被从上至下解释。</li> <li>只要是不使用 <code>async</code> / <code>defer</code> 属性的 <code>&lt;script&gt;</code> 元素都会阻塞浏览器解析后面的内容。</li> <li>可以使用 DOM API 动态创建以异步方式加载 <code>&lt;script&gt;</code> 元素。</li></ol> <div class="custom-block warning"><p class="custom-block-title">注意事项</p> <ul><li>使用行内 JavaScript 代码时，字符串 <code>&lt;/script&gt;</code> 需要进行转义，正确书写为 <code>&lt;\/script&gt;</code>。</li> <li>解释外部 JavaScript 文件时，阻塞时间也包含下载文件的时间。</li> <li>当在使用 src 属性的 <code>&lt;script&gt;</code> 元素中再包含 JavaScript 代码时，浏览器只会下载并执行外部脚本文件，忽略行内代码。</li> <li>只有使用 <code>type</code> 属性值为 <code>module</code> 的 <code>&lt;script&gt;</code> 元素内才能使用 ES6 模块语法。</li></ul></div> <h2 id="noscript-元素"><a href="#noscript-元素" class="header-anchor">#</a> <code>&lt;noscript&gt;</code> 元素</h2> <p><code>&lt;noscript&gt;</code> 元素被用于给不支持或禁用 JavaScript 的浏览器提供替代内容，从而实现优雅降级。它可以包含除了 <code>&lt;script&gt;</code> 以外任何可以出现在 <code>&lt;body&gt;</code> 中的 HTML 元素。</p> <h2 id="最佳实践"><a href="#最佳实践" class="header-anchor">#</a> 最佳实践</h2> <ul><li>尽可能将 JavaScript 代码放在外部文件中，按需通过 <code>&lt;script&gt;</code> 标签对 JavaScript 代码进行分发，比如 <a href="https://www.cdnetworks.com/what-is-a-cdn/" target="_blank" rel="noopener noreferrer">Content Delivery Network (CDN)<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>。</li> <li>最好将 <code>&lt;script&gt;</code> 标签放在 <code>&lt;body&gt;</code> 元素中的页面内容后面，防止阻塞浏览器，以减少白屏时间。</li> <li>尽量不使用异步脚本，如需使用则最好不要在加载期间修改 DOM，因为页面可能还未渲染完。</li> <li>通过 DOM API 添加的脚本可以通过显式设置 <code>async</code> 属性值为 <code>false</code> 改为同步脚本，此时最好在文档头部显示声明 <code>&lt;link rel=&quot;preload&quot; href=&quot;xxx.js&quot;&gt;</code> 标签，以减少它带来的性能影响。</li></ul></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/Fengfengfeng-up/personal-docs/edit/main/docs/my-docs/js/javascript-in-html.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">12/31/2020, 5:05:56 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/my-docs/js/what-is-javascript.html" class="prev">
        什么是 JavaScript
      </a></span> <span class="next"><a href="/my-docs/js/language-basics.html">
        语言基础
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.e45e7f79.js" defer></script><script src="/assets/js/4.1dc261f9.js" defer></script><script src="/assets/js/31.04c169d6.js" defer></script>
  </body>
</html>
